/*
 * $Id: Resolve.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.core.api;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.p4eclipse.core.api.ClientUser;
import net.sourceforge.p4eclipse.core.api.IPerforceCommand;
import net.sourceforge.p4eclipse.core.api.IResolveResult;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser object for the 'p4 resolve' command.
  * 
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Resolve extends ClientUser implements IPerforceCommand
{
	private List result = new ArrayList();
	private IStatus status;
	
	/*** Convenience method for creating instances of Resolve. */
	public static final Resolve newInstance()
	{
		MultiStatus status = new MultiStatus(PerforceUIPlugin.ID, IStatus.OK, "", null); //$NON-NLS-1$
		return new Resolve(status);
	}

	/** Creates a new instance. */
	private Resolve(MultiStatus status)
	{
		super(status);
		this.status = status;
	}	

	/**
	  * @see IClientUser#outputInfo(char, String)
	  */
	public void outputInfo(char level, String infoString)
	{
		if (level != '0')
			return;
			
		int pos = infoString.lastIndexOf(" - ");                                //$NON-NLS-1$
		if (pos != -1)
		{
			Result resolveResult = new Result();
			resolveResult.filename = infoString.substring(0, pos);
			result.add(resolveResult);
		}
	}

	/**
	  * Returns the name of the p4 command of this instance.
	  */
	public String getCommand()
	{
		return "resolve";                                                       //$NON-NLS-1$
	}
	
	/**
	  * Returns the result of the operation.
	  */
	public IResolveResult[] getResult()
	{
		return (IResolveResult[])result.toArray(new IResolveResult[result.size()]);
	}

	/**
	  * Returns the IStatus object associated with the Fstat instance.
	  */
	public IStatus getStatus()
	{
		return status;
	}	
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}	
	
	/**
	  * Implementation of the IResolveResult interface. Stores information
	  * provided by the 'p4 resolve' command. 
	  * 
	  * @see IResolveResult
	  */
	public class Result implements IResolveResult
	{
		private String filename;
		
		/** @see IResolveResult#getLocalFilename() */
		public String getLocalFilename()
		{
			return filename;
		}
	}	
}
